#include <iostream>
#include <fstream>
#include "DocItem.h"

std::map<std::string, TermStat> gTerms;
std::map<std::string, ClassStat> docClasses;
std::map<unsigned int, Cluster> clusters;
unsigned int docCount;

void load(char* filename);
void loadClass(char* filename);
void stats();

int main (int argc, char** const argv)
{
	docCount = 0;
	if (argc == 5)
	{
		//Order matters, but not a big deal for the moment
		int d = 2;
		if (strcmp(argv[d-1], "-b") == 0)
		{
			load(argv[d++]);
			d++;
		}
		if (strcmp(argv[d-1], "-c") == 0)
		{
			loadClass(argv[d++]);
			d++;
		}
		stats();
	}
	else
	{
		std::cout<< "Must provide saved term data \"-b filename\" ";
		std::cout<< "and saved class data \"-c filename\"\n";
	}
}

void load(char* filename)
{
	std::ifstream inFile(filename);
	if (inFile == NULL)
	{
		std::cout<< "Invalid term file\n";
		return;
	}
	//std::cout<< "Loading\n";
	
	std::string term = "";
	unsigned int dCount, tCount;
	std::string classf;
	float idf;
	char x;
	
	while (!inFile.eof())
	{
		inFile >> x;
		if (x == '+')
		{
			inFile >> term >> tCount >> dCount >> idf;
			gTerms[term].count = tCount;
			gTerms[term].dCount = dCount;
			gTerms[term].idf = idf;
		}
		else if (x == '-')
		{
			inFile >> classf >> tCount;
			gTerms[term].cCounts[classf] = tCount;
			docClasses[classf].seen = true;
		}
		else if (x == '=')
		{
			inFile >> tCount;
			docCount+= tCount;
		}
	}
	inFile.close();
}

void loadClass(char* filename)
{
	std::ifstream inFile(filename);
	if (inFile == NULL)
	{
		std::cout<< "Invalid class file\n";
		return;
	}
	//std::cout<< "Loading class\n";
	
	std::string term = "";
	unsigned int count, dCount, tCount, size, cls;
	double p;
	std::string classf;
	float idf, w;
	char x;
	
	while (!inFile.eof())
	{
		inFile >> x;
		if (x == '+')
		{
			inFile >> classf >> size >> tCount >> cls >> p;
			docClasses[classf].termSize = size;
			docClasses[classf].dCount = tCount;
			docClasses[classf].cluster = cls;
			docClasses[classf].point = p;
			docClasses[classf].seen = true;
			
			if (clusters.count(cls) == 0)
			{
				double np = clusters[cls].meanPoint * clusters[cls].count + p;
				clusters[cls].count++;
				clusters[cls].meanPoint = np/clusters[cls].count;
			}
			else
			{
				clusters[cls].count = 1;
				clusters[cls].meanPoint = p;
			}
		}
		else if (x == '-')
		{
			inFile >> term >> count >> dCount >> idf >> w;
			docClasses[classf].charTerms[term].count = count;
			docClasses[classf].charTerms[term].dCount = dCount;
			docClasses[classf].charTerms[term].idf = idf;
		}
	}
	inFile.close();
}

void stats()
{
	std::cout<< "Total Document Count: " << docCount << "\n";
	
	std::cout<< "\nCLUSTER INFORMATION\n";
	std::cout<< "Cluster Count: " << clusters.size() << "\n";
	std::map<unsigned int, Cluster>::iterator aItr = clusters.begin();
	while (aItr != clusters.end())
	{
		std::cout<< "Cluster: " << aItr->first << "\n";
		std::cout<< " Mean Point: " << aItr->second.meanPoint << "\n";
		std::cout<< " Mean Class: " << aItr->second.meanClass << "\n";
		std::cout<< " Class Count: " << aItr->second.count << "\n";
		std::cout<< " Classes:\n";
		
		std::map<std::string, ClassStat>::iterator cItr = docClasses.begin();
		unsigned int dCount = 0;
		while (cItr != docClasses.end())
		{
			if (cItr->second.cluster == aItr->first)
			{
				std::cout<< "  " << cItr->first << " " << cItr->second.dCount << "\n";
				dCount+= cItr->second.dCount;
			}
			cItr++;
		}
		std::cout<< " Document Count: " << dCount << "\n";
		
		aItr++;
	}
	
	std::cout<< "\nCLASS INFORMATION\n";
	std::cout<< "Class Count: " << docClasses.size() << "\n";
	std::map<std::string, ClassStat>::iterator cItr = docClasses.begin();
	while (cItr != docClasses.end())
	{
		std::cout<< "Class: " << cItr->first << "\n";
		std::cout<< " Document Count: " << cItr->second.dCount << "\n";
		std::cout<< " Cluster: " << cItr->second.cluster << "\n";
		cItr++;
	}
	
	std::cout<< "\nTERM INFORMATION\n";
	std::cout<< "Term Count: " << gTerms.size() << "\n";
	std::map<std::string, TermStat>::iterator tItr = gTerms.begin();
	while (tItr != gTerms.end())
	{
		tItr++;
	}
}
